/**
 * Script XML Http Service
 * Use XML to invoke a remote method
 * @author yehailong
 */
function test1(){
	alert(globalContextPath);
} 
function xmlhttp(url){

    var xh = new ActiveXObject("MSXML2.XMLHTTP");
    xh.open("get",url,false);
    xh.setRequestHeader("Content-Type","gb2312");
    xh.send();

    if(xh.readyState == 4){
      	return xh.responseText;
       // resultDiv.innerHTML = xh.responseText;
    }

}

/** xmlhttp servlet*/
var xmlHttpURI = globalContextPath + "/XmlHttp";

/**
 * Use javascript to invoke a remote method .
 * The invoked method can have a return value,
 * but when the method has return value the value must be a array of String?String[]??
 * 
 * HOW TO USE:
 *    var response = new methodInvoke(param0[,param1[,param2][,...]]);
 * PARAMETERS SPICIFICATION:
 *    param0 : NEED
 *             Indicate the class name.
 *    param1...paramN : YOUR CHOICE
 *             Decided by the method your invoked. 
 *             Have the same sequence with the method you invoked.
 * RETURN VALUE:
 *    Decided by the method your invoke(a array of string).
 * example:
 *    var response = new methodInvoke("gyywService.queryDBMethod","zhangfei","20050905");
 */
function methodInvoke(){
	var params = new Array(methodInvoke.arguments.length);
	for(var i=0 ; i<methodInvoke.arguments.length;i++){
		params[i] = methodInvoke.arguments[i];
	}

	var url = buildURL(params);
	var doc = URLRequest(url);
	var response = parseResponse(doc);
	if(response != null){
		for(var i=0 ; i<response.length;i++){
			this[i] = response[i];
		}
	}
}

/**
 * @serviceName spring service name
 * @methodName the service's method name
 * @properties return properties,split with "," and not space,such as "id,name,remarks"
 * @params the method's parameters
 */
function invokeService(serviceName, methodName, properties, params){
	var result = new methodInvoke(serviceName, methodName, properties, params);
	var rowCount = parseInt(result[0]);
	var returnResult = new Array(1+rowCount);
	returnResult[0] = rowCount;
	for(i=1; i<rowCount + 1; i++){
		var row = result[i];
		returnResult[i] = row.split(";");
	}
	return returnResult;
}

function invokeServiceStringReturn(serviceName, methodName, properties, params){
	var result = new methodInvoke(serviceName, methodName, properties, params);
	var rowCount = parseInt(result[0]);
	var returnResult = "";
	for(i=1; i<rowCount + 1; i++){
		var row = result[i];
		row.replace(";",",");
		returnResult += ","+row.replace(";",",");
	}
	return returnResult;
}

function getI18n(key, params){
	if(!params) params = "void";
	var res = invokeServiceStringReturn("sys", "i18n", key, params);
	if(res.length > 0)
		if(res.substring(0,1) == ',') res = res.substring(1);
	return res;
}

function setSessionValue(key, value){
	new methodInvoke("","setSession",key, value);
}

function getSessionValue(key){
	var res = new methodInvoke("session",  key);
	return res[1];
}

/**
 * Use javascript to invoke a remote method which is configured in Spring FrameWork .
 * The invoked method can have a return value,
 * but when the method has return value the value must be a string(xml string) or a xml Document(dom4j)
 * 
 * HOW TO USE:
 *    var xmldoc = new XMLDocRMI(param0[,param1[,param2][,...]]);
 * PARAMETERS SPICIFICATION:
 *    param0 : NEED
 *             Indicate the service name and the method name which are separated by ".".
 *    param1...paramN : YOUR CHOICE
 *             Decided by the method your invoked. 
 *             Have the same sequence with the method you invoked.
 * RETURN VALUE:
 *    a "ActiveXObject("MSXML2.DOMDocument")" object as the method returned
 * example:
 *    var xmldoc = new XMLDocRMI("gyywService.queryDBMethod","zhangfei","20050905");
 */
function XMLDocRMI(){

	var params = new Array(XMLDocRMI.arguments.length);
	
	for(var i=0 ; i<XMLDocRMI.arguments.length;i++){
		params[i] = XMLDocRMI.arguments[i];
	}

	var url = buildURL(params);
	var xmldoc = URLRequest(url);
	return xmldoc;
	
}

function buildURL(paraArray){
	var url = xmlHttpURI;
	if(paraArray.length<2){
		return false;
	}
	url += "?serviceName="+paraArray[0]+"&methodName="+paraArray[1]+"&properties="+paraArray[2];
	for(var i=3; i<paraArray.length;i++){
		url = url + "&param=" + paraArray[i];
	}
	return url;
}

function URLRequest(url){
	var xmldoc = new ActiveXObject("MSXML2.DOMDocument");
	xmldoc.async=false;
	xmldoc.load(url);
	var err = xmldoc.parseError;
	if(err.errorCode!=0){
		return null;
	}else{
		return xmldoc;
	}
}

function parseResponse(xmldoc){
	if(xmldoc==null){
		return null;
	}else{
	
		var node = xmldoc.selectSingleNode("/response/size");
		var size = 0;
		if(node != null){
	    	size = eval(node.text);
	    }
	    
	    if(size == 0) 	return null;
	    	
	    var response = new Array(size);
	    	
	    for(var i=0;i<size;i++){
	    	node = xmldoc.selectSingleNode("/response/infor[@id='"+i+"']");
	    	if(node != null)
	    		response[i] = node.text;
	    }
	    
	    return response;
	}
}

function sendXMLRequest(url,xmldoc){
	req = new ActiveXObject("MSXML2.XMLHTTP");   
    req.open("POST", url, false);
	req.send(xmldoc);
}

function uploadXml(xml,fileName){

	var xmldoc = new ActiveXObject("MSXML2.DOMDocument");
	xmldoc.async=false;
    xmldoc.loadXML(xml);
    
    var url = xmlHttpURI+"?mode=upload&fileName="+fileName;
	sendXMLRequest(url,xmldoc);
}